package com.zjw.zy.componse.demo.view

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Divider
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.onFocusChanged
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.zjw.zy.componse.demo.R

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2023/2/16 11:16
 * @description：Compose通用UI
 */

@Composable
@Preview
fun CommonEditView(
    modifier: Modifier = Modifier,
    value: String = "",
    @DrawableRes leftIcon: Int = -1,
    onValueChange: (String) -> Unit = {},
    hint: String = "",
    enabled: Boolean = true,
    readOnly: Boolean = false,
    textStyle: TextStyle = TextStyle.Default,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    singleLine: Boolean = true,
    maxLines: Int = Int.MAX_VALUE,
    visualTransformation: VisualTransformation = VisualTransformation.None,
    onTextLayout: (TextLayoutResult) -> Unit = {},
    interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
    cursorBrush: Brush = SolidColor(Color.Black),
) {

    var hasFocus by remember { mutableStateOf(false) }
    var text by remember { mutableStateOf(value) }

    BasicTextField(value = text, onValueChange = {
        text = it
        onValueChange.invoke(it)
    }, modifier = modifier.onFocusChanged {
        hasFocus = it.hasFocus
    }, singleLine = singleLine
    ) { innerTextField ->
        Row(
            modifier = Modifier.fillMaxSize(), verticalAlignment = Alignment.CenterVertically
        ) {
            if (leftIcon != -1) {
                Image(
                    painter = painterResource(id = leftIcon),
                    contentDescription = null,
                    modifier = Modifier
                        .weight(1f)
                        .wrapContentSize()
                )
                Spacer(modifier = Modifier.width(2.dp))
            }


            Box(
                modifier = Modifier
                    .weight(10f)
                    .fillMaxSize()
            ) {
                if (text.isEmpty()) {
                    Text(
                        text = hint,
                        color = Color.Gray,
                        modifier = Modifier.align(Alignment.CenterStart)
                    )
                }
                Box(modifier = Modifier.align(Alignment.CenterStart)) {
                    innerTextField()
                }

                Divider(
                    modifier = Modifier
                        .fillMaxWidth()
                        .align(Alignment.BottomCenter),
                    thickness = 2.dp
                )

            }
            if (hasFocus && text.isNotEmpty()) {
                Image(
                    painter = painterResource(id = R.drawable.icon_delete),
                    contentDescription = null,
                    modifier = modifier
                        .size(30.dp, 30.dp)
                        .clickable {
                            text = ""
                            onValueChange(text)
                        },
                )
            }

        }
    }
}